{% set vuln_view = vulnerability_details.vulnerability_view %}

{% set commit_hash = vulnerability_details.commit_hash %}
{% set repo_url = vulnerability_details.repo_url %}


{% macro print_link(value) -%}
  {% if value is string and value.startswith('http') %}
    <a href="{{ value }}" class="text-info" target="_blank">{{ value }}</a>
  {% elif value is iterable and value is not string %}
    {{ print_link(value[0]) }}
  {% else %}
    {{ value }}
  {% endif %}
{%- endmacro %}

{% macro print_formatted(data, expand_label) -%}
  {% if data is iterable and data is not string and data|length > 1 %}
    {% set max_limit = 5 %}

    {% for entry in data %}
      {% if loop.index0 < max_limit %}
        &#8226; {{ print_link(entry) }} <br/>
      {% endif %}
    {% endfor %}

    {% if data|length > max_limit %}
      {% set remaining = data|length - max_limit %}
      <div>
        <a href="#" class="text-light" data-toggle="collapse" data-target="#expand_{{ expand_label }}">
          <i class="fa fa-caret-square-o-right" aria-hidden="true"></i>
          More/Less ({{ remaining }})
        </a>
      </div>
      <div id="expand_{{ expand_label }}" class="collapse">
    {% endif %}

    {% for entry in data %}
      {% if loop.index0 >= max_limit %}
        &#8226; {{ print_link(entry) }} <br/>
      {% endif %}
    {% endfor %}

    {% if data|length > max_limit %}
      </div>
    {% endif %}

  {% else %}
    {{ print_link(data) }}
  {% endif %}
{% endmacro %}


<div class="card">
  <div class="card-body">
    {% if vuln_view %}
    {% set vuln_id = vuln_view.id %}

    <h5 class="card-title">
      ID:
      <p class="text-info d-inline">
        <b>{{ vuln_view.id }}</b>
        {% if vuln_view.cve_id %}
        <small>(<a href="https://nvd.nist.gov/vuln/detail/{{ vuln_view.cve_id }}" target="_blank">NVD</a>)</small>
        {% endif %}
      </p>
      - Vulnerability Info
      <small><a href="{{ url_for('vuln.create_vuln', vuln_id=vuln_id) }}">(edit)</a></small>
    </h5>

    <h6 class="card-subtitle mb-2 text-muted">{{ vuln_view.date_created }}</h6>

    <div class="row mb-1">
      <div class="col-sm-12">
        <p>
        {% if vuln_view.comment %}
          {{ vuln_view.comment }}
        {% else %}-{% endif %}
        </p>
      </div>
    </div>

    {% set master_commit = vuln_view.master_commit %}
    <div class="table-responsive">
        <table class="table table-hover table-dark mb-0">
          <tr>
            <td style="width: 7%"><b>Products</b></td>
            {% set products = vuln_view.products %}

            <td>
              {% if products %}
                {{ print_formatted(products, 'products') }}
              {% elif master_commit.repo_name %}
                {{ master_commit.repo_name }} (repository)
              {% else %}
                N/A
              {% endif %}
            </td>
          </tr>

          {% if vuln_view.cwe_id %}
          <tr>
            <td><b>Type</b></td>
            <td>
              <b>{{ vuln_view.cwe_name or "?" }}</b> (<a href="http://cwe.mitre.org/data/definitions/{{ vuln_view.cwe_id | replace("CWE-", "") }}.html" class="text-info" target="_blank">{{ vuln_view.cwe_id }}</a>)
            </td>
          </tr>
          {% endif %}

          {# TODO: Decide if we want to display the NVD score.
            {% if vuln_view.score %}
            <tr>
              <td><b>Score</b></td>
              <td>
                {{ '%0.1f' % vuln_view.score }}
              </td>
            </tr>
            {% endif %}
          #}

          <tr>
            <td>
              <b>First patch</b>
              {% if vuln_view.master_commit_date %}
                <br />{{ vuln_view.master_commit_date }}
              {% endif %}
            </td>
            <td style="word-break:break-all;">
              {% if master_commit %}
                {% set master_commit_link = master_commit.commit_link %}
                <b><a href="{{ master_commit_link }}" class="text-info" target="_blank">{{ master_commit_link }}</a></b><br />
                {% if vuln_view.master_commit_message %}
                  {% set maximum_message_words = 50 %}
                  {% set message_words = vuln_view.master_commit_message.split(' ')  %}
                  {% if message_words | length > maximum_message_words %}
                    "<b>{{ message_words[:maximum_message_words]|join(' ') }}...</b>"
                    {% set remaining_message = message_words[maximum_message_words:]|join(' ')  %}
                    <div>
                      <a href="#" class="text-light" data-toggle="collapse" data-target="#expand_commit_message">
                        <i class="fa fa-caret-square-o-right" aria-hidden="true"></i>
                        More/Less ({{ remaining_message.split("\n")|length }})
                      </a>
                    </div>
                    <div id="expand_commit_message" class="collapse">
                      <b><div style="white-space: pre-wrap;">{{ remaining_message }}</div></b><br />
                    </div>
                  {% else %}
                    "<b>{{ vuln_view.master_commit_message }}</b>"<br />
                  {% endif %}
                  <br />
                  Stats: <b>+{{ vuln_view.master_commit_stats.additions }} lines / -{{ vuln_view.master_commit_stats.deletions }} lines (total: {{ vuln_view.master_commit_stats.total }} lines)</b><br />
                {% endif %}
              {# TODO: add more commit information: message, #modified files and date?
                  <i class="fa fa-check" style="color: #00FF00"></i>
                  <table class="mb-0">
                    <tr>
                      <td class="py-0" style="width: 5%"><b>FOO</b></td>
                      <td class="py-0" style="width: 95%"><b>BAR</b></td>
                    </tr>
                    <tr>
                      <td class="py-0" style="width: 5%"><b>FOO</b></td>
                      <td class="py-0" style="width: 95%"><b>BAR</b></td>
                    </tr>
                  </table>
                #}
              {% else %}
                <i class="fa fa-times" style="color: #FF0000"></i> None
              {% endif %}
            </td>
          </tr>

          {% set patches = vuln_view.known_patches %}
          {% if patches %}
          <tr>
            <td><b>Patches</b></td>
            <td style="word-break:break-all;">{{ print_formatted(vuln_view.known_patches, 'patches') }}</td>
          </tr>
          {% endif %}

          {% set relevant_files = vuln_view.relevant_files %}
          {% if relevant_files %}
          <tr>
            <td><b>Relevant file/s</b></td>
            <td style="word-break:break-all;">{{ (relevant_files and print_formatted(relevant_files, 'relevant-files')) }}</td>
          </tr>
          {% endif %}

          {% set links = vuln_view.link_references %}
          {% if links %}
            <tr>
              <td><b>Links</b></td>
              <td style="word-break:break-all;">{{ (links and print_formatted(links, 'links')) }}</td>
            </tr>
          {% endif %}

          <tr>
            <td><b>Annotation</b></td>
            <td>
              {% if not vuln_view.master_commit %}
                <b><p class="text-warning d-inline">Note:</p></b>
                <p class="d-inline">No patch was assigned yet.</p>
              {% elif vuln_view.master_commit.comments|length == 0 %}
                <b><p class="text-warning d-inline">Note:</p></b>
                <p class="d-inline"><b>This entry has not been annotated yet.</b></p>
                Please consider adding data:
                <a href="{{ url_for('vuln.vuln_editor', vuln_id=vuln_id) }}">
                <button type="button" class="btn-primary btn-tb">Annotate entry</button>
                </a>
              {% else %}
                <a href="{{ url_for('vuln.vuln_editor', vuln_id=vuln_id) }}">
                <button type="button" class="btn-primary btn-tb">Edit annotation</button>
                </a>
              {% endif %}
            </td>
          </tr>


        </table>
      </div>

      {% if vuln_view.additional_resources %}
        <h5>Additional Resources</h5>
        <div class="list-group">
          {% for resource_link in vuln_view.additional_resources %}
            <a href="#" class="list-group-item">{{ resource_link.link }}</a>
          {% endfor %}
        </div>
      {% endif %}

    {% else %}
    <form action="{{ url_for('vuln.create_vuln', vuln_id=None) }}" method="post">
        <input type="hidden" name="id" value="{{vuln_id}}">
        <input type="hidden" name="csrf_token" value="{{ csrf_token() }}"/>
      {% if commit_link %}
      Commit Link:
      <h5>
        <a href="{{ commit_link }}" target="_blank">{{ commit_link }}</a>
      </h5>
      <input type="hidden" name="commits-0-commit_link" value="{{ commit_link }}">
      {% endif %}
      {% if repo_url and commit_hash %}
      Repository:
      <h5>{{ repo_url }}</h5>
      Commit:
      <h5>{{ commit_hash }}</h5>
      <input type="hidden" name="commits-0-repo_url" value="{{ repo_url }}">
      <input type="hidden" name="commits-0-commit_hash" value="{{ commit_hash }}">
      {% else %}
      <h5>{{ vuln_id }}</h5>
      {% endif %}
        <div class="alert alert-primary" role="alert">
        No entry for this data found.
        <button type="submit" class="btn btn-link">Create new entry</button>
        </div>
      </form>
    {% endif %}
  </div>
</div>
